package poketrade;

import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import poketrade.entities.User;
import poketrade.managers.DealManager;
/**
 * Servlet implementation class AnnonceServlet
 */
@WebServlet("/AnnonceServlet")
public class AnnonceServlet extends BaseHttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	private DealManager mgr;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String s = request.getParameter("prixCarte");
		String n = request.getParameter("carteSelec");
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		try {
			float credit = Float.valueOf(s);
			if (credit>0){
				u = mgr.publishOffer(u.getId(),n,credit);
				if (u == null)
				{
					showMessage(request, response, "Format invalide, pas cool !");
					return;
				}
				session.setAttribute("user", u);
			}
			showMessage(request, response, "Annonce postée avec succès !");
		}
		catch (Exception e){
			showMessage(request,response,"Format invalide, boo !");
		}
	}
		



}
